package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.OverflowListGoodsDao;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.OverflowList;
import com.atguigu.jxc.entity.OverflowListGoods;
import com.atguigu.jxc.service.OverflowListGoodsService;
import com.google.gson.Gson;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OverflowListGoodsServiceImpl implements OverflowListGoodsService {
    @Autowired
    private OverflowListGoodsDao overflowListGoodsDao;

    /**
     * 报溢单查询
     */
    @Override
    public List<OverflowList> list(String sTime, String eTime) {
        return overflowListGoodsDao.selectDamageList(sTime,eTime);
    }

    /**
     * 报溢单商品信息
     * @return
     */
    @Override
    public List<OverflowListGoods> goodsList(Integer overflowListId) {
        return overflowListGoodsDao.goodsList(overflowListId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(String overflowNumber, String overflowDate, String remarks, String overflowListGoodsStr, Integer userID) {
        OverflowList overflowList = new OverflowList();
        overflowList.setOverflowNumber(overflowNumber);
        overflowList.setOverflowDate(overflowDate);
        overflowList.setRemarks(remarks);
        overflowList.setUserId(userID);
        overflowListGoodsDao.saveOverflowList(overflowList);
        //damageListGoodsStr转换
        Gson gson = new Gson();
        DamageListGoods[] goodsArray = gson.fromJson(overflowListGoodsStr, DamageListGoods[].class);
        for (DamageListGoods goods : goodsArray) {
            overflowListGoodsDao.saveDamageListGoods(overflowList.getOverflowListId(),goods);
        }
    }
}
